package router

import (
	"iris-wms/apps/admin/web/service"
	"iris-wms/common/model"
	"iris-wms/common/tools/consts"
	"iris-wms/common/tools/result"

	"github.com/kataras/iris/v12"
)

func NewWmsWarehouseRoute() *wmsWarehouseRoute {
	return &wmsWarehouseRoute{}
}

type wmsWarehouseRoute struct{}

func (r *wmsWarehouseRoute) Create(ctx iris.Context, service_ service.WmsWarehouseService) {
	var in model.WmsWarehouse
	if err := ctx.ReadJSON(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	in.Uid = ctx.Values().GetUintDefault(consts.UID, 0)
	if err := service_.Create(in); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *wmsWarehouseRoute) Edit(ctx iris.Context, service_ service.WmsWarehouseService) {
	var in model.WmsWarehouse
	if err := ctx.ReadJSON(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err := service_.Update(in); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *wmsWarehouseRoute) Delete(ctx iris.Context, service_ service.WmsWarehouseService) {
	var idList = make([]uint, 0)
	if err := ctx.ReadJSON(&idList); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err := service_.DeleteByIdList(idList); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *wmsWarehouseRoute) List(ctx iris.Context, service_ service.WmsWarehouseService) {
	word := ctx.URLParam("word")
	rows, err := service_.GetList(word)
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OKData(ctx, rows)
}
